package yu.zhou.njupt.pdsmigration.processers.impl;

import java.io.File;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

import org.apache.commons.dbutils.QueryRunner;

import yu.zhou.njupt.pdsmigration.processers.Processor;

/**
 * @author yu.zhou
 * FIXME  这个没起任何作用
 */
public class PartProcessor implements Processor {

    public void process(Connection connection) throws Exception {
        Properties config = new Properties();
        InputStream configFile = getClass().getResourceAsStream("/config.properties");
        config.load(configFile);
        String insertString = "INSERT INTO NEW_EPC_PART_IMAGE (PART_NO, FILE_NAME) VALUES(?, ?)";
        String[][] data = null;
        String imagePathString = config.getProperty("sourcePath") + "part_image";
        File imagePath = new File(imagePathString);
        QueryRunner sqlRunner = new QueryRunner();
        
        try {
            data = new String[imagePath.list().length][];
            int dataIndex = 0;
            String partNo = null;
            for (String imageName : imagePath.list()) {
                if (imageName.contains("_")) {
                    partNo = imageName.substring(0, imageName.lastIndexOf("_"));
                    data[dataIndex] = new String[]{partNo, imageName};
                    dataIndex++;
                }else {
                    partNo = imageName.substring(0, imageName.lastIndexOf("."));
                    data[dataIndex] = new String[]{partNo, imageName};
                    dataIndex++;
                }
            }
            sqlRunner.batch(connection, insertString, data);
            System.out.println("PartProcessor Complete!");
        } catch (Exception e) {
            System.out.println("PartProcessor Failed!");
            throw e;
        }
    }

}
